package com.qf.front.controller;

import com.qf.front.biz.GoodsBiz;
import com.qf.ym.entity.GoodsEntity;
import com.qf.ym.entity.ResponseResult;
import com.qf.ym.finalstr.FruitsFinal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Controller
public class ShopCarController {

    @Autowired
    GoodsBiz goodsBiz;
    //添加商品到购物车
    @RequestMapping("addShopCar")
    @ResponseBody
    public ResponseResult addShopCar(int gid, HttpSession session){

        ResponseResult rr = new ResponseResult();

        //添加到购物车;

        // 1 : 得到自己的购物车  商品id(key) 商品信息(value)
        Map<Integer, GoodsEntity> shopCar = (Map<Integer, GoodsEntity>) session.getAttribute("shopCar");

        if(null == shopCar){//说明第一次往购物车里面添加东西;
            shopCar = new HashMap<>(); //没有购物车,new一个购物车;
        }

        String thiscount="";//保存的就是当前商品的总价格

        //2: 往购物车里面添加商品了; 两种情况[第一次添加这个商品][之前已经加过这个商品了,只要对商品的数量+1就行了]

        if(null == shopCar.get(gid)){ //如果当前商品是第一次添加
            //2.1 查询到这个商品
            GoodsEntity goodsEntity = goodsBiz.queryGoodsDetail(gid);
            //2.2加到购物车
            shopCar.put(gid,goodsEntity);

            thiscount+= goodsEntity.getGoodsPrice();

        }else{ //存在这个商品,直接对数量进行+1
            GoodsEntity goodsEntity = shopCar.get(gid);
            goodsEntity.setGoodsNumber(goodsEntity.getGoodsNumber()+1);

            thiscount += goodsEntity.getGoodsPrice()*goodsEntity.getGoodsNumber();
        }

        //3: 保存购物车;
        session.setAttribute("shopCar",shopCar);

        //4: 把当前商品的数量,当前商品的总价格;返回到前台展示;  当前操作商品的总价

        rr.setResult(FruitsFinal.SUCCESS_RESULT);
        rr.setResultMsg(getNumberAndPrirce(shopCar)+","+thiscount);
        return rr;
    }
   /* //添加商品
    @RequestMapping("addShopCar")
    public ResponseResult addShopCar(int gid, HttpSession session){
        ResponseResult rr = new ResponseResult();

        Map<Integer, GoodsEntity> shopCar = (Map<Integer, GoodsEntity>) session.getAttribute("shopCar");
        if(null==shopCar){
            shopCar=new HashMap<>();//没有创建一个新的购物车
        }
         String thiscount="";//商品单价
        //购物车中没有此商品
        if(null == shopCar.get(gid)){
            GoodsEntity ge = goodsBiz.queryGoodsDetail(gid);
            shopCar.put(gid,ge);
            thiscount+=ge.getGoodsPrice();
        }else{//购物车中存在此商品
            GoodsEntity ge1=shopCar.get(gid);
            ge1.setGoodsNumber(ge1.getGoodsNumber()+1);//商品数加一
            thiscount+=ge1.getGoodsPrice()*ge1.getNumber();
        }

            session.setAttribute("shopChar",shopCar);
            rr.setResult(FruitsFinal.SUCCESS_RESULT);
            rr.setResultMsg(getNumberAndPrirce(shopCar)+","+thiscount);

            return rr;
    }*/
   @RequestMapping("subShopCar")
   @ResponseBody
   public ResponseResult subShopCar(int gid, HttpSession session){
       ResponseResult rr = new ResponseResult();

       Map<Integer,GoodsEntity> shopCar=(Map<Integer, GoodsEntity>) session.getAttribute("shopCar");

       if (shopCar.get(gid)==null){
           rr.setResult(FruitsFinal.FILED_RESULT);

       }

       String thiscount="";
       //当购物车物品大于1
       if(shopCar.get(gid).getGoodsNumber()>1){
           GoodsEntity ge=shopCar.get(gid);
           ge.setGoodsNumber(ge.getGoodsNumber()-1);
           thiscount+=ge.getGoodsPrice()*ge.getGoodsNumber();
           rr.setResult("9000");//操作成功
       }else {
           shopCar.remove(gid);
           rr.setResult("9001");//操作成功
       }

       session.setAttribute("shopCar",shopCar);

       rr.setResultMsg(getNumberAndPrirce(shopCar)+","+thiscount);

       return rr;


   }
   /* public String getNumberAndPrice(Map<Integer,GoodsEntity> shopCar){
        if(shopCar==null){
            return null;
        }
        float count=0;
        int   number=0;

        GoodsEntity ge;
        Iterator<GoodsEntity> iterator = shopCar.values().iterator();
        while (iterator.hasNext()){
             ge = iterator.next();
            count+= ge.getGoodsPrice()*ge.getGoodsNumber();
            number+=ge.getGoodsNumber();

        }
        return number+","+count;

    }*/

      //清空购物车

    @RequestMapping("cleanShopCar")
    public String clearShopCar(HttpSession session){

        Map<Integer,GoodsEntity> shopCar=(Map<Integer,GoodsEntity>)session.getAttribute("shopCar");

       session.removeAttribute("shopCar");

       return "/shopcart";


    }


    //得到商品的数量和总价格
    private String getNumberAndPrirce(Map<Integer, GoodsEntity> shopCar){

        if(null == shopCar){
            return "0,0.00";
        }

        Iterator<GoodsEntity> iterator = shopCar.values().iterator();

        GoodsEntity ge;
        int numberCount = 0; //商品数量
        float priceCount = 0; //商品总价
        while(iterator.hasNext()){
            ge = iterator.next();
            numberCount += ge.getGoodsNumber();

            priceCount += ge.getGoodsNumber() * ge.getGoodsPrice();
        }

        return numberCount+","+priceCount;
    }
}
